package cn.sanch.user.controller;

import cn.sanch.basic.annotation.PreAuthorize;
import cn.sanch.user.dto.UserDto;
import cn.sanch.basic.util.BusinessException;
import cn.sanch.user.dto.UserEmailDto;
import cn.sanch.user.service.IUserService;
import cn.sanch.user.domain.User;
import cn.sanch.user.query.UserQuery;
import cn.sanch.basic.util.AjaxResult;
import cn.sanch.basic.util.PageList;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import java.util.List;

/**
* 后端接口类；
*/
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    public IUserService userService;

    /**
     * 接口：添加或修改
     * @param user  传递的实体
     * @return AjaxResult 响应给前端
     */
    @PutMapping

    public AjaxResult addOrUpdate(@RequestBody User user){
        try {
            if( user.getId()!=null) {
                userService.update(user);
            }else {
                userService.save(user);
            }
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：删除
    * @param id
    * @return AjaxResult 响应给前端
    */
    @DeleteMapping(value="/{id}")

    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            userService.delete(id);
            return AjaxResult.me();
        } catch (Exception e) {
        e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：批量删除
    * @param ids
    * @return AjaxResult 响应给前端
    */
    @PatchMapping

    public AjaxResult patchDelete(@RequestBody Long[] ids){
        try {
            userService.patchDelete(ids);
            return AjaxResult.me();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("操作失败！");
        }
    }

    /**
    * 接口：查询单个对象
    * @param id
    */
    @GetMapping("/{id}")

    public User queryOne(@PathVariable("id")Long id) {
        return userService.findOne(id);
    }


    /**
    * 接口：查询所有
    * @return
    */
    @GetMapping

    public List<User> queryAll(){
        return userService.findAll();
    }


   /**
    * 接口：分页查询或高级查询
    * @param query 查询对象
    * @return PageList 分页对象
    */
    @PostMapping

    public PageList<User> queryPage(@RequestBody UserQuery query) {
        return userService.queryPage(query);
    }


    /**
     * 接口：手机号注册
     * @param userDto
     * @return
     */
    @PostMapping("/phone/register")
    public AjaxResult phoneRegister(@RequestBody UserDto userDto) {
        try {
            userService.phoneRegister(userDto);
            return AjaxResult.me();
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败！" + b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }

    /**
     * 接口：邮箱注册
     * @param userEmailDto
     * @return
     */
    @PostMapping("/email/register")
    public AjaxResult emailRegister(@RequestBody UserEmailDto userEmailDto) {
        try {
            userService.emailRegister(userEmailDto);
            return AjaxResult.me();
        } catch (BusinessException b) {
            b.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("注册失败！" + b.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setMessage("系统繁忙！");
        }
    }
}
